﻿using System;
using System.Collections.Generic;
 
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Net.Security;

namespace GGSharp
{
    public class GGConnection
    {
        protected const string USER_AGENT = "Mozilla/5.0 (Windows; U; Windows NT 6.0; pl; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4 (.NET CLR 3.5.30729)";

        protected TcpClient _client = new TcpClient();

        protected string _host;
        protected int _port;
        protected ConnectionState _state = ConnectionState.Disconnected;

        public event EventHandler Connected;
        public event EventHandler Disconnected;

        public string HostName
        {
            get { return _host; }
        }

        public int HostPort
        {
            get { return _port; }
        }

        public bool IsConnected
        {
            get { return _state == ConnectionState.Connected; }
        }

        internal TcpClient Socket
        {
            get { return _client; }
        }

        internal Stream Stream
        {
            get { return _client.GetStream(); }
        }

        public ConnectionState State
        {
            get { return _state; }
        }

        protected void Connect(string hostName, int port)
        {
            _client.NoDelay = true;
            _client.BeginConnect(hostName, port, new AsyncCallback(clientConnected), null);
        }

        public void Connect(int uin)
        {
            string url = "/appsvc/appmsg4.asp?fmnumber=" + uin + "&version=7.7.0.3315";
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://appmsg.gadu-gadu.pl" + url);
            request.UserAgent = USER_AGENT;
            StreamReader responseStream = new StreamReader(request.GetResponse().GetResponseStream());

            string responseContent = responseStream.ReadToEnd();
            string[] lines = responseContent.Split(' ');
            lines = lines[2].Split(':');

            string host = lines[0];
            int port = Int32.Parse(lines[1]);

            Connect(host, port);
        }

        private void clientConnected(IAsyncResult ar)
        {
            _state = ConnectionState.Connected;
            OnConnected();
        }

        public void Disconnect()
        {
            if (IsConnected)
            {
                _state = ConnectionState.Connecting;
                _client.Client.Close();
                _client.Close();
                OnDisconnected();
            }
            else
            {
            }
        }

        protected virtual void OnConnected()
        {
            if (Connected != null)
                Connected(this, EventArgs.Empty);
        }

        protected virtual void OnDisconnected()
        {
            if (Disconnected != null)
                Disconnected(this, EventArgs.Empty);
        }
    }
}
